import re
import requests
# headers = {
#     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
# }
res = requests.get('http://book.km.com/')
text = res.text
# 找到所有符合条件的结果
# rea = re.findall('<a.*?target=".*?">(.*?)</a>', text, re.S)
# print(rea)
# for result in rea:
#     print(result)
# sub方法，替换
# content = 'renshuo 1234 renshuo 134'
# content = re.sub('\d+','替换后的字符串',content)
# print(content)
# # 如果替换的字符串要加字符
# content = 'renshuo 1234 renshuo 134'
# content1 = re.sub('(\d+)', r'\1 890', content)
# print(content1)
# html = re.sub('<a.*?>|</a>', '', text) # 去掉a标签
# res = re.findall('<p.*?>(.*?)</p>', html, re.S)
# for i in res:
#     print(i)
# re.compile复用
content = 'hello 1234567 world_this is a regex demo'
pattern = re.compile('.*', re.S)
res = re.match(pattern, content)
# res = re.match('.*',content,re.S)
print(res)
